const CryptoJS = require("crypto-js"); //引用AES源码js

/**
 * 解密
 * @param {String} word
 * @return {Object}
 */
function decrypt(word, aeskey,mode = 'ECB',padding = 'Pkcs7',iv = '') {
  var key = CryptoJS.enc.Utf8.parse(aeskey);
  var _iv = CryptoJS.enc.Utf8.parse(iv);
  var decrypt = CryptoJS.AES.decrypt(word, key, {
    iv:_iv,
    mode: CryptoJS.mode[mode],
    padding: CryptoJS.pad[padding]
  });

  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

/**
 * 加密
 * @param {string} word
 * @returns {string}
 */
function encrypt(word, aeskey,mode = ECB,padding = 'Pkcs7',iv = '') {
  var key = CryptoJS.enc.Utf8.parse(aeskey);
  var srcs = CryptoJS.enc.Utf8.parse(word);
  var _iv = CryptoJS.enc.Utf8.parse(iv);
  var encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv:_iv,
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });

  return encrypted.toString();
}

// let code = encrypt(JSON.stringify(123))
// console.log('123加密后：', code)
// console.log('123解密后：', decrypt(code))

export { encrypt, decrypt };
